package com.aplink.generic.network;

import java.io.File;

import android.os.Parcel;
import android.os.Parcelable;

public class GenericFile implements Parcelable {
	public static final Parcelable.Creator<GenericFile> CREATOR = new Parcelable.Creator<GenericFile>() {

		@Override
		public GenericFile createFromParcel(final Parcel source) {
			return new GenericFile(source);
		}

		@Override
		public GenericFile[] newArray(final int size) {
			return new GenericFile[size];
		}
	};
	private boolean isCanDelete = true;
	private String mExtension;
	private String mLocalPath;
	private String mName;

	private String mPhysicalPath;

	public GenericFile() {

	}

	private GenericFile(Parcel source) {
		mExtension = source.readString();
		mLocalPath = source.readString();
		mName = source.readString();
		mPhysicalPath = source.readString();
		source.readBooleanArray(new boolean[] { isCanDelete });
	}

	public boolean delete() {
		if (this.mLocalPath != null) {
			final File file = new File(this.mLocalPath);
			if (file.exists()) {
				return file.delete();
			}
		}
		return false;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	public String getExtension() {
		return this.mExtension;
	}

	public String getLocalPath() {
		return this.mLocalPath;
	}

	public String getName() {
		return this.mName;
	}

	public String getPhysicalPath() {
		return this.mPhysicalPath;
	}

	public File getRealFile() {
		return new File(this.mLocalPath);
	}

	public boolean isCanDelete() {
		return this.isCanDelete;
	}

	public void setCanDelete(final boolean isCanDelete) {
		this.isCanDelete = isCanDelete;
	}

	public void setExtension(final String extension) {
		this.mExtension = extension;
	}

	public void setFileNameByURl(final String url) {
		final File file = new File(url);
		if (file.exists()) {
			this.mName = file.getName();
		}
	}

	public void setLocalPath(final String localPath) {
		this.mLocalPath = localPath;
	}

	public void setName(final String name) {
		this.mName = name;
	}

	public void setPhysicalPath(final String physicalPath) {
		this.mPhysicalPath = physicalPath;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// dest.writeStringArray(new String[] { mExtension, mLocalPath, mName,
		// mPhysicalPath });
		dest.writeString(mExtension);
		dest.writeString(mLocalPath);
		dest.writeString(mName);
		dest.writeString(mPhysicalPath);
		dest.writeBooleanArray(new boolean[] { isCanDelete });
	}
}
